package inglar.autotc.client.ui;

import inglar.autotc.client.Utilities;
import inglar.autotc.client.resource.ResourceHandler;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PageActions extends Composite {
	private VerticalPanel contentPanel;
	private FlexTable actionsTable;

	public PageActions() {
		
		contentPanel = new VerticalPanel();
		initWidget(contentPanel);
		
		contentPanel.add(new Image(ResourceHandler.INSTANCE.fbLoadBlue()));
		
		actionsTable = new FlexTable();
		actionsTable.setCellPadding(5);
		
		AsyncCallback<String[]> getActionListCallback = new AsyncCallback<String[]>() {
			@Override
			public void onSuccess(String[] result) {
				loadActionList(result);
			}
			@Override
			public void onFailure(Throwable caught) {
				Utilities.failureAlert("getActionListCallback", "RPC failure", caught);
			}
		};
		
		String userid = Cookies.getCookie("QID");
		Utilities.rpc.getActionList(userid, getActionListCallback);
	}

	protected void loadActionList(String[] result) {
		contentPanel.clear();
		
		contentPanel.add(actionsTable);
		
		HTML htmlPrio = new HTML("<b>Priority</b>");
		actionsTable.setWidget(0, 0, htmlPrio);
		
		HTML htmlDetect = new HTML("<b>Detect</b>");
		actionsTable.setWidget(0, 1, htmlDetect);
		
		HTML htmlAction = new HTML("<b>Action</b>");
		actionsTable.setWidget(0, 2, htmlAction);
		
		if(result.length == 0) {
			actionsTable.setWidget(1, 0, new HTML("<b style='color: red'>No actions available.</b>"));
		} else {
			for(int i = 0; i < result.length; i++) {
				
				String[] t = result[i].split("#");
				final String actionid = t[0].trim();
				final String prio = t[1].trim();
				final String detect = t[2].trim();
				final String action = t[3].trim();
				
				actionsTable.setWidget(i + 1, 0, new HTML(prio));
				actionsTable.setWidget(i + 1, 1, new HTML(detect));
				actionsTable.setWidget(i + 1, 2, new HTML(action));
				
				final Button btnDelRow = new Button("<b>x</b>");
				actionsTable.setWidget(i + 1, 3, btnDelRow);
				btnDelRow.addClickHandler(new ClickHandler() {
					@Override
					public void onClick(ClickEvent event) {
						
						Utilities.showLoadingModal();
						
						AsyncCallback<String> delActionCallback = new AsyncCallback<String>() {
							@Override
							public void onSuccess(String result) {
								
								Utilities.hideLoadingModal();
								
								if(result.trim().isEmpty()) {
									Window.alert("Successfully deleted action.");
									int numRows = actionsTable.getRowCount();
									for(int i = 1; i < numRows; i++) {
										if(actionsTable.getWidget(i, 3) == btnDelRow) {
											actionsTable.removeRow(i);
											break;
										}
									}
								} else {
									Window.alert(result.trim());
									Location.reload();
								}
							}
							@Override
							public void onFailure(Throwable caught) {
								Utilities.failureAlert("delClientCallback", "RPC failure", caught);
								Location.reload();
							}
						};
						
						String userid = Cookies.getCookie("QID");
						Utilities.rpc.deleteAction(userid, actionid, delActionCallback);
					}
				});
			}
		}
		
		Button btnAddRow = new Button("Add Action");
		contentPanel.add(btnAddRow);
		btnAddRow.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				DialogBox db = new DialogBoxAddAction();
				db.center();
			}
		});
	}

}
